from .utils import *
from .serializer import *
from .modelsutils import *
from .permission import NeedPermission
from rest_framework.views import APIView
from .paging import PagePagination
from rest_framework.response import Response


class AllUser(APIView):
    """
    查询用户的所有 报安信息
    """
    throttle_scope = 'admin'
    permission_classes = [NeedPermission]
    
    def get(self, request):
        users = Detail_info.objects.get_queryset().order_by('id')
        page = PagePagination()
        page_users = page.paginate_queryset(queryset=users, request=request, view=self)
        users_ser = Detail_infoSerializer(instance=page_users, many=True)
        # return Response(users_ser.data)  # 只返回数据
        return page.get_paginated_response(users_ser.data)  # 返回前后页面页面url


class AllOrder(APIView):
    """
    获取所有订单信息
    """
    throttle_scope = 'admin'
    permission_classes = [NeedPermission]

    def get(self, request):
        order = OrderList.objects.get_queryset().order_by('id')
        page = PagePagination()
        page_order = page.paginate_queryset(queryset=order, request=request, view=self)
        order_ser = OrderListSerializer(instance=page_order, many=True)

        return page.get_paginated_response(order_ser.data)


class CodePage(APIView):
    """
    获取所有 invcode 信息
    """
    throttle_scope = 'admin'
    permission_classes = [NeedPermission]

    def get(self, request):
        invcode = InvCode.objects.get_queryset().order_by('id')
        page = PagePagination()
        page_invcode = page.paginate_queryset(queryset=invcode, request=request, view=self)
        invcode_ser = InvCodeSerializer(instance=page_invcode, many=True)
        return page.get_paginated_response(invcode_ser.data)


class GenCode(APIView):
    """
    生成新的 邀请码 信息
    """
    throttle_scope = 'admin'
    permission_classes = [NeedPermission]

    def post(self, request):
        capacity = request.data.get('capacity')
        tips = request.data.get('tips')
        code = gencode()
        InvCode_create(code, capacity, tips)
        return Response(
            data={
                'success': 'yes',
            }
        )


class CodeInfo(APIView):
    """
    查询给定 code 的信息
    """
    throttle_scope = 'admin'
    permission_classes = [NeedPermission]

    def post(self, request):
        code = request.data.get('code')
        info = InvCode_filter_code(code)
        if info == '该激活码不存在!':
            return Response(
                data={
                    'error': info,
                }
            )
        else:
            return Response(
                data={
                    'msg': info,
                }
            )


class CodeOrderInfo(APIView):
    """
    根据code查询使用该code的用户信息
    """
    throttle_scope = 'admin'
    permission_classes = [NeedPermission]

    def post(self, request):
        code = request.data.get('code')
        # 根据 code 查找使用 该 code 的用户昵称， 注意 code 不存在的情况
        info = OrderList_filter_code_username(code)
        if info == '该邀请码不存在!':
            return Response(
                data={
                    'error': info,
                }
            )
        else:
            return Response(
                data={
                    'msg': info,
                }
            )


class EditCode(APIView):
    """
    更新 code 的信息
    """
    throttle_scope = 'admin'
    permission_classes = [NeedPermission]

    def post(self, request):
        code = request.data.get('code')
        capacity = request.data.get('capacity')
        tips = request.data.get('tips')
        # 修改 code 的信息，注意 code 不存在的情况
        info = InvCode_filter_code_edit(code, capacity, tips)
        if info == '该激活码不存在!':
            return Response(
                data={
                    'error': info,
                }
            )
        else:
            return Response(
                data={
                    'success': 'yes',
                }
            )


class DelCode(APIView):
    """
    删除code, 实质为 将 valid 设置为 0
    """
    throttle_scope = 'admin'
    permission_classes = [NeedPermission]

    def post(self, request):
        code = request.data.get('code')
        # 修改 code 的 valid 为 0 ，注意 code 不存在的情况
        info = InvCode_filter_code_edit_valid(code)
        if info == '该激活码不存在!':
            return Response(
                data={
                    'error': info,
                }
            )
        else:
            return Response(
                data={
                    'success': 'yes',
                }
            )


class DelUser(APIView):
    """
    删除user, 实质为 将 is_active 设置为 0
    设置 detailinfo 的start 字段为0
    """
    throttle_scope = 'admin'
    permission_classes = [NeedPermission]

    def post(self, request):
        username = request.data.get('username', '')
        if username != '':
            # 查询该 username 是否已经注册
            info = User_filter_username(username)
            # 查询该 username 是否已经提交报安信息
            info1 = Detail_info_filter_username(username)
            if info == 0:
                return Response(data={'error': '该账号尚未注册！'})
            #  如果用户注册了就禁用登录
            # 将该 username 的 is_active 设置为 0
            User_filter_username_modify_is_active_1(username)
            if info1 == 0:
                return Response(
                    data={'error': '该用户尚未提交详细报安信息，无法停止服务，已冻结账号！'})
            # 如果用户还有报平安信息就停止服务
            # 设置 Detail_info 的start 字段为0
            Detail_info_filter_modify_start(username)
            return Response(
                data={
                    'success': 'yes',
                }
            )
        else:
            return Response(
                data={
                    'error': '接口请求错误！',
                }
            )


class UserInfo(APIView):
    """
    根据 username 查询用户的信息
    """
    throttle_scope = 'admin'
    permission_classes = [NeedPermission]

    def post(self, request):
        username = request.data.get('username', '')
        if username != '':
            # 查询该 username 是否已经注册
            info = User_filter_username(username)
            # 查询该 username 是否已经提交报安信息
            info1 = Detail_info_filter_username(username)
            if info == 0:
                return Response(
                    data={
                        'error': '该账号尚未注册！',
                    }
                )
            if info1 == 0:
                return Response(
                    data={
                        'error': '该用户尚未提交详细报安信息，暂时无法查询用户信息！',
                    }
                )
            # 查询 权限、状态信息
            is_staff, is_active = User_filter_info(username)
            # 查询 报安信息
            info2 = Detail_info_filter_username_info(username)
            info2['is_staff'] = is_staff
            info2['is_active'] = is_active
            return Response(
                data={
                    'data': info2,
                }
            )
        else:
            return Response(
                data={
                    'error': '接口请求错误！',
                }
            )


class EditUserInfo(APIView):
    """
    编辑用户信息
    """
    throttle_scope = 'admin'
    permission_classes = [NeedPermission]

    def post(self, request):
        username = request.data.get('username', '')
        password = request.data.get('password', '')
        staff = request.data.get('staff', '')
        active = request.data.get('active', '')
        print(username + password + staff + active)
        print(f"active....:{type(active)}")
        # 查询该 username 是否已经注册
        info = User_filter_username(username)
        # 查询该 username 是否已经提交报安信息
        # 这里无需判断，此接口只改变user表，全面封禁使用deluser接口，减少重复功能
        # info1 = Detail_info_filter_username(username)
        if info == 0:
            return Response(
                data={
                    'error': '该账号尚未注册！',
                }
            )
        # 只需要判断用户是否注册，即可修改用户密码
        if password != '':
            # 修改用户密码
            User_modify_password(username, password)
        # 只需要判断用户是否注册，即可修改用户权限
        if staff != '':
            # 修改用户权限
            User_modify_is_staff(username, staff)
        # 判断用户已经注册，且已经填写了报安信息  才可以修改用户状态
        if active != '':
            # 修改用户状态
            # 将该 username 的 is_active 设置为 0 或 1 用来解禁或者封禁(仅仅是User中 is_active 的修改)
            User_filter_username_modify_is_active_1(username, code=active)
        return Response(
            data={
                'success': 'yes',
            }
        )
